package org.example.game.history;

import java.util.Arrays;

/**
 * @Description: TODO
 * @Author wyatt
 * @Data 2024/05/19 21:10
 */
public class Solution3153 {

    public static void main(String[] args) {
        Solution3153 solution100300 = new Solution3153();
        int[] nums2 = new int[]{106,453};
        System.out.println(solution100300.sumDigitDifferences2(nums2));

    }

    //通过
    public long sumDigitDifferences2(int[] nums) {
        if(nums.length == 1){
            return 0;
        }
        long count = 0;

        int[] num = new int[10];
        while(String.valueOf(nums[0]).length() > 1){
            for(int i=0;i<nums.length;i++){
                num[nums[i]%10]++;
                nums[i] = nums[i]/10;
            }
            for(int i=0;i<num.length;i++){
                if(num[i] > 0){
                    count += (nums.length-num[i]) * num[i];
                }
            }
            Arrays.fill(num, 0);
        }

        for(int i=0;i<nums.length;i++){
            num[nums[i]]++;
        }

        for(int i=0;i<num.length;i++){
            if(num[i] > 0){
                count += nums.length-num[i];
            }
        }
        return count/2;
    }

    //未通过 超时
    public long sumDigitDifferences(int[] nums) {
        long num = 0;
        for(int i=0;i<nums.length-1;i++){
            for(int j=i+1; j<nums.length;j++){
                long difNum = calDif(nums[i], nums[j]);
                num += difNum;
                System.out.println(nums[i] + "," + nums[j] + "," + difNum);
            }
        }
        return num;
    }

    private long calDif(int num, int num1) {
        if(num == num1){
            return 0;
        }
        long count = 0;
        while (num >= 10){
            count += (num%10 == num1%10) ? 0 : 1;
            num = num/10;
            num1 = num1/10;
        }
        count += (num == num1) ? 0 : 1;
        return count;
    }

}
